#include "animation.h"
#include <iostream>
using namespace krryn::animation;

animation::animation(krryn::view::model *a_Model, float a_FrameRate, unsigned int a_NumFrames)
	: m_FrameRate(a_FrameRate),
	  m_Model(a_Model),
	  m_NumFrames(a_NumFrames), 
	  m_LastTime(0), 
	  m_MaxTime(1.f / a_FrameRate), 
	  m_CurrentFrame(0), 
	  m_NextFrame(1)
{
	m_Skeletons = new skeleton*[m_NumFrames];
}

animation::~animation(){
	delete[] m_Skeletons;
}

void animation::dump(){
	std::cout
		<< "num-frames: " << m_NumFrames << "\n"
		<< "last-time: " << m_LastTime << "\n"
		<< "max-time: " << m_MaxTime << "\n"
		<< "current-frame: " << m_CurrentFrame << "\n"
		<< "next-frame: " << m_NextFrame << std::endl;
}

void animation::update(float a_DT){
	dump();

	// Advance the animation, currently assumes that it's looping
	int l_MaxFrames = m_NumFrames - 1;
	m_LastTime += a_DT;

	if(m_LastTime >= m_MaxTime){
		m_CurrentFrame++;
		m_NextFrame++;
		m_LastTime = 0.f;

		if(m_CurrentFrame > l_MaxFrames)
			m_CurrentFrame = 0;

		if(m_NextFrame > l_MaxFrames)
			m_NextFrame = 0;
	}
}

void animation::set_skeleton(size_t i, skeleton *a_Skeleton){
	m_Skeletons[i] = a_Skeleton;
}

skeleton *animation::get_current() const { 
	return m_Skeletons[m_CurrentFrame]; 
}

skeleton *animation::get_next() const { 
	return m_Skeletons[m_NextFrame];
}